package com.qlmjx.sevalopos;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.qlmjx.sevalopos.base.BaseActivity;
import com.qlmjx.sevalopos.https.HttpAddress;
import com.qlmjx.sevalopos.https.RequestParams;
import com.qlmjx.sevalopos.utils.ConstantsUtils;
import com.universal.frame.generalutils.GeneralUtil;
import com.universal.frame.generalutils.JsonUtil;
import com.universal.frame.generalutils.SharedPreferencesUtil;
import com.universal.frame.generalutils.ToastUtil;
import com.universal.frame.okhttp.OKHttpUtils;
import com.universal.frame.okhttp.ResultCallBack;

import java.io.IOException;

import okhttp3.Request;

/**
 * Created by sa on 2018/7/26.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener ,ResultCallBack {
    private EditText  ed_admin;
    private EditText  ed_password;
    private TextView tv_login;
    private long firstTime;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_login);

        //不弹出软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    }

    @Override
    public void initView() {
        ed_admin= (EditText) findViewById(R.id.ed_admin);
        ed_password= (EditText) findViewById(R.id.ed_password);
        tv_login = (TextView) findViewById(R.id.tv_login);
        tv_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String admin =ed_admin.getText().toString().trim();
        String password = ed_password.getText().toString().trim();
        if (TextUtils.isEmpty(admin)){
            ToastUtil.showToast(this, "账号不能为空！");
            return;
        }
        if (TextUtils.isEmpty(password)){
            ToastUtil.showToast(this, "密码不能为空！");
            return;
        }
        getLoginInfo(admin,password);
    }
        /**
     * 登陆
     */
    public void getLoginInfo(String username, String password) {
        OKHttpUtils.postAsync(this
                , HttpAddress.LOGIN_URL, ""
                , new RequestParams(this).getLoginParams(username, password)
                , this
                , false, 1);
    }

    @Override
    public void onSuccess(String response, int flag) {
        try {
            if (flag == 1) {
                getLoginData(response);
            }

        } catch (Exception e) {
            GeneralUtil.tryShow(getApplicationContext(), e);
        }

    }

    private void getLoginData(String response) {
        String en = JsonUtil.getString(response, "en", "");
        String flag = JsonUtil.getString(en, "flag", "");
        if ("1".equals(flag)){
            String systemUser = JsonUtil.getString(en, "systemUser", "");
            String userId= JsonUtil.getString(systemUser, "userId", "");
            String companyId= JsonUtil.getString(systemUser, "companyId", "");
            String deptId= JsonUtil.getString(systemUser, "deptId", "");
            SharedPreferencesUtil.putString(this, ConstantsUtils.USER_ID,userId);
            SharedPreferencesUtil.putString(this, ConstantsUtils.COMPANY_ID,companyId);
            SharedPreferencesUtil.putString(this, ConstantsUtils.DEPT_ID,deptId);
            SharedPreferencesUtil.putBoolean(this, ConstantsUtils.IS_LOGIN, true);
            startActivity(new Intent(this, MainActivity.class));
            this.finish();
        }else {
            ToastUtil.showToast(this, "账号或者密码错误！");
        }
    }

    @Override
    public void onFailure(Request request, IOException e, int flag) {

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (System.currentTimeMillis() - firstTime > 2000) {
                Toast.makeText(LoginActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                firstTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
